﻿using System;

namespace LibMSNP.Client
{
    public enum States
    {
            DISCONNECTED = 0,
            INITIATING = 1,
            AUTHENTICATING = 2,
            AUTHENTICATED = 3,
            LOGGED_IN = 4 
    }

    public class MSNPClientStateMap
    {
        private class StateNode
        {
            private States _state;
            private StateNode _previous;
            private StateNode _next;

            public StateNode(States state)
            {
                _state = state;
            }

            public States State
            {
                get { return _state; }
            }

            public void SetNextState(ref StateNode next)
            {
                _next = next;
            }

            public void SetPreviousState(ref StateNode previous)
            {
                _previous = previous;
            }

            public StateNode GetNextState()
            {
                return _next;
            }

            public StateNode GetPreviousState()
            {
                return _previous;
            }
        }
        StateNode _start;
        StateNode _current;

        public MSNPClientStateMap()
        {
            _start = new StateNode(States.DISCONNECTED);
            StateNode second = new StateNode(States.INITIATING);
            StateNode third = new StateNode(States.AUTHENTICATING);
            StateNode fourth = new StateNode(States.AUTHENTICATED);
            StateNode fifth = new StateNode(States.LOGGED_IN);            
            fifth.SetNextState(ref _start);
            fifth.SetPreviousState(ref _start);
            fourth.SetNextState(ref fifth);
            fourth.SetPreviousState(ref _start);
            third.SetNextState(ref fourth);
            third.SetPreviousState(ref _start);
            second.SetNextState(ref third);
            second.SetPreviousState(ref _start);
            _start.SetNextState(ref second);
            _start.SetPreviousState(ref _start);

            _current = _start;
        }

        public States NextState()
        {
            _current = _current.GetNextState();
            return _current.State;
        }

        public States PreviousState()
        {
            _current = _current.GetPreviousState();
            return _current.State;
        }

        public States CurrentState
        {
            get { return _current.State; }
        }
    }
}
